package service

import (
	"encoding/json"
	"fmt"
	"nt-order/common"
	"nt-order/conf"
)

type ShopWebLogic struct {
	
}

type CountryInfo struct {
	Code string `json:"code"`
	Zh_name string `json:"zh_name"`
	En_name string `json:"en_name"`
	User_name string `json:"user_name"`
	Create_time string `json:"create_time"`
}

func (m *ShopWebLogic)GetCountryNameInfo(countryCode string) *CountryInfo {
	redisKey:=common.GetCacheKey("ORDER_SHOP_COUNTRY_INFOS")
	redis:=conf.GetRedis()

	exists:= redis.Exists(redisKey)
	if exists==0 {
		type Resp struct {
			Msg string `json:"msg"`
			Code int `json:"code"`
			Data struct{
				Counties []CountryInfo
			}
		}
		resp:=Resp{}
		curl, err := common.Curl("GET", fmt.Sprintf("%s/shop/country?code=&zhName=&enName=", conf.Env.Url.ShopUrl), nil, nil)
		if err != nil {
			panic(err)
		}
		err = json.Unmarshal(curl, &resp)
		if err != nil {
			panic(err)
		}

		tt:=make(map[string]string)
		for _, county := range resp.Data.Counties {
			tmp, err := json.Marshal(county)
			if err != nil {
				panic(err)
			}
			if len(county.Code)>0 {
				tt[county.Code]=string(tmp)
			}
		}
		err = redis.HMset(redisKey,tt)
		if err != nil {
			panic(err)
		}
	}

	res:=CountryInfo{}
	data, err := redis.Hget(redisKey,countryCode)
	if err != nil || data=="" {
		return nil
	}

	err = json.Unmarshal([]byte(data), &res)
	if err != nil {
		panic(err)
	}

	return &res
}